#include "i2c_aht10.h"
#include "menu.h"
#include "AppLayer.h"
#include "heating.h"

#include <string.h>

#define TEMP_X 0
#define TEMP_Y 32
#define NUM_12_32 4

SLIDER_str * slider_y_set = NULL;

MENU_C_STR menu = {
  .layer = MENU_LAYER_SW,
  .function = FUNCTION_STATE,
  .work = NO_WORK,
  .set_data = SET_T1_TEMP,
  .set_time = SET_TEMP,
  .window_cnt = 0,
  .current_row = 0,
  .dis_buf = {0},
};

const char * menu_function_str[] = {
    " ",            
    "T1",
    "T2",
    "T3",
    "B1",
    "B2",
    "B3",
    "S1",
    "S2",
    "S3",
};

#define BLINK_OFF()     (menu.set_blink_light = BLINK_CYCLE+2)
#define BLINK_ON()      (menu.set_blink_light = BLINK_LIGHT)
#define BLINK_CLE()     (menu.set_blink_cnt = 0)


void menu_init(void)
{
   slider_y_set = menu_slider_y_create(SET_LABLE_Y_LENG);
   menu_seting_data_init(); 
}

void menu_reset(void)
{
    menu.layer = MENU_LAYER_SW;
    menu.layer_last = MENU_LAYER_SW;
    menu.function = FUNCTION_STATE;
    menu.work = NO_WORK;
    menu.set_data = SET_T1_TEMP;
    menu.set_time = SET_TEMP;
    menu.window_cnt = 0;
    menu.current_row = 0;
    menu.set_data_ok = 0;
    menu.set_time_ok = 0;
    menu.set_work_ok = 0;
    menu.set_blink_cnt = 0;
    menu.set_blink_light = BLINK_CYCLE+2;
    memset(menu.dis_buf, 0, sizeof(menu.dis_buf));

    menu_seting_data_init(); 

}

uint8_t data_to_str_V(uint16_t data, char * str) 
{
    uint8_t temp = 0;
    uint8_t point = 0;

    temp = data / 1000;
    if(temp >= 10)
    {
        temp = temp / 10;
        str[point] = temp + '0';   
        point++;
        str[point] = (data/1000) - (temp * 10) + '0';  
        point++;
    }
    else
    {
        str[point] = temp + '0';
        point++;
    }

    str[point] = '.';
    point++;
    temp = (data % 1000) / 100;
    str[point] = temp + '0';
    
    point++;
    str[point] =  'V';
    point++;
    str[point] =  '\0';

    return (point);
}

uint8_t data_to_str_8(int8_t data, char * str)
{
    uint8_t bai = 0;
    uint8_t shi = 0;
    uint8_t ge = 0;
    uint8_t temp = 0;

    if(data < 0)
    {
        str[temp] = '-';
        data = -data;
        temp++;
    }

    while (data >= 100)
    {
        data = data - 100;
        bai++;
    }
    while (data >= 10)
    {
        data = data - 10;
        shi++;
    }
    ge = data;

    if(bai > 0) 
    {
        str[temp] = bai + '0';
        str[temp+1] = shi + '0';
        str[temp+2] = ge + '0';  
        str[temp+3] = '\0';  
        return (temp+3);
    }
    else if(shi > 0)
    {
        str[temp] = shi + '0';
        str[temp+1] = ge + '0';
        str[temp+2] = '\0';
        return (temp+2);
    }
    else
    {
        str[temp] = ge + '0';
        str[temp+1] = '\0';
        return (temp+1);
    }
}

uint8_t data_to_str_16(int16_t data, char * str)
{
    uint8_t w = 0;
    uint8_t q = 0;
    uint8_t b = 0;
    uint8_t s = 0;
    // uint8_t i = 0;
    uint8_t temp = 0;
    if(data < 0)
    {
        data = -data;
        str[temp] = '-';
        temp++;
    }
    while (data >= 10000)
    {
        data = data - 10000;
        w++;
    }
    while (data >= 1000)
    {
        data = data - 1000;
        q++;
    }
    while (data >= 100)
    {
        data = data - 100;
        b++;
    }
    while (data >= 10)
    {
        data = data - 10;
        s++;
    }
    if(w > 0)
    {
        str[temp] = '0' + w;
        str[temp+1] = '0' + q;
        str[temp+2] = '0' + b;
        str[temp+3] = '0' + s;
        str[temp+4] = '0' + data;
        str[temp+5] = '\0';
        return (temp+5);
    }
    else if (q > 0)
    {
        str[temp] = '0' + q;
        str[temp+1] = '0' + b;
        str[temp+2] = '0' + s;
        str[temp+3] = '0' + data;
        str[temp+4] = '\0';
        return (temp+4);
    }
    else if(b > 0)
    {
        str[temp] = '0' + b;
        str[temp+1] = '0' + s;
        str[temp+2] = '0' + data;
        str[temp+3] = '\0';
        return (temp+3);
    }
    else if(s > 0)
    {
        str[temp] = '0' + s;
        str[temp+1] = '0' + data;
        str[temp+2] = '\0';
        return (temp+2);
    }
    else
    {
        str[temp] = '0' + data;
        str[temp+1] = '\0';
        return (temp+1);
    }
}

uint8_t str_add_str_char(char* str_s, char str_char)
{
    uint8_t i = 0;

    while (str_s[i] != '\0')
    {
        i++;
    }

    str_s[i] = str_char;
    i++;
    str_s[i] = '\0';
    return i;
}

uint8_t time_to_str(uint16_t time, char * str)
{
    uint8_t house = 0;
    uint8_t minute = 0;
    uint8_t s = 0;
    uint8_t i = 0;
    if(time > 60*60)
    {
        house = time / 3600;
        minute = (time - (house * 3600)) / 60;
        if(house >= 10)
        {
            str[i++] = house / 10 + '0';
            str[i++] = house % 10 + '0';        
        }
        else
        {
            str[i++] = house + '0';
        }
        str[i++] = ':';
        str[i++] = minute / 10 + '0';
        str[i++] = minute % 10 + '0';
        str[i++] = '\0';
    }
    else
    {
        minute = time / 60;
        s = time % 60;
        if(minute >= 10)
        {
            str[i++] = minute / 10 + '0';
            str[i++] = minute % 10 + '0';        
        }
        else
        {
            str[i++] = minute + '0';
        }
        str[i++] = ':';
        str[i++] = s / 10 + '0';
        str[i++] = s % 10 + '0';
        str[i++] = '\0';

    }
    return i;
}

void menu_data_update(void)
{
    menu.power_volatage = Heating_Platform.voltage;
}

void menu_function_create(uint8_t _function)
{
    uint8_t leng = 0;
    uint8_t _x_vlaue = 0;

    if(Heating_Platform.actual_temp >= 100)
    {
        _x_vlaue = 90;
    }
    else
    {
        _x_vlaue = 80;
    }

    if(_function > NO_WORK)
    {
        if(_function <= HEATING_B3)
        {
            menu.set_temp = Heating_Platform.stHeating_Control[_function-CONTROL_T1].set_control_temp;        
        }
        else if(_function <= REFLOW_S3)
        {
            menu.set_temp = Heating_Platform.stReflowSoldering[_function-REFLOW_S1].stHeating_Phases[2].set_control_temp;        
        }
        
        leng = data_to_str_16(menu.set_temp,menu.dis_buf);
        oled_show_dis_string((SET_TEMP_X+NUM_12_32),(SET_TEMP_Y),NUMBER_1624,menu.dis_buf,leng);                  
        oled_show_dis_char((SET_TEMP_X + 16 * leng + NUM_12_32 + 2),(SET_TEMP_Y + 24 - 16),CELSIUS_1616,0);      
        menu.work_time = Heating_Platform.heat_time;  
        leng = time_to_str(menu.work_time,menu.dis_buf);
        oled_show_dis_string(80,16,ASC2_0816,menu.dis_buf,leng);   

        menu.power_value = Heating_Platform.power_control;
        leng = data_to_str_16(menu.power_value,menu.dis_buf);
        leng  = str_add_str_char(menu.dis_buf,'%');
        oled_show_dis_string(90,32,ASC2_0816,menu.dis_buf,leng);     

        if(menu.set_blink_cnt < menu.set_blink_light)
        {
            oled_show_dis_string(90,48,ASC2_0816,menu_function_str[_function],2);      
        }
    }
    else
    {
        oled_show_dis_string((SET_TEMP_X+NUM_12_32),(SET_TEMP_Y),ASC2_0816,"SET TEMP",8); 
        oled_show_dis_string(80,16,ASC2_0816,"Time",4); 
        oled_show_dis_string(_x_vlaue,32,ASC2_0816,"Power",5);
        oled_show_dis_string(_x_vlaue,48,ASC2_0816,"Mode",4);      
    }
    
    menu.temp = Heating_Platform.actual_temp;
    leng = data_to_str_16(menu.temp,menu.dis_buf);

    oled_show_dis_string(TEMP_X,TEMP_Y,NUMBER_2032,menu.dis_buf,leng);
    oled_show_dis_char((TEMP_X + 20*leng),(TEMP_Y + 32 - 24),CELSIUS_2424,0);
    
    menu.power_volatage = Heating_Platform.voltage;
    leng = data_to_str_V(menu.power_volatage,menu.dis_buf);
    oled_show_dis_string(80,0,ASC2_0816,menu.dis_buf,leng);
}

#if 1  

#include <stdlib.h>

#define SLIDER_MODE_X       0
#define SLIDER_MODE_Y       1
#define SLIDER_X            0
#define SLIDER_Y            0
#define SLIDER_Y_LEN        (64-1)
#define SLIDER_X_LEN        (128-1)
#define SLIDER_WIDE         5
#define SLIDER_BOX_LEN      8
#define SLIDER_FLASH_S_MAX  10
#define SLIDER_FLASH_S_MIN  1
#define SLIDER_FLASH_X      8       
#define SLIDER_FLASH_Y      4

SLIDER_str * menu_slider_y_create(uint8_t _value)
{
    SLIDER_str * slider_y = malloc(sizeof(SLIDER_str));

    if(slider_y != NULL)
    {
        slider_y->x_y = SLIDER_MODE_Y;
        slider_y->x = SLIDER_X_LEN - SLIDER_WIDE;
        slider_y->y = SLIDER_Y;
        slider_y->len = SLIDER_Y_LEN;
        slider_y->wide = SLIDER_WIDE;
        slider_y->value = 0;  
        slider_y->value_max = _value; 
        slider_y->box_len = SLIDER_BOX_LEN;
        slider_y->box_speed = SLIDER_FLASH_S_MAX;
    }
    return slider_y;
}

SLIDER_str * menu_slider_x_create(uint8_t _value)
{
    SLIDER_str * slider_x = malloc(sizeof(SLIDER_str));
    if(slider_x!= NULL)
    {
        slider_x->x_y = SLIDER_MODE_X;
        slider_x->x = SLIDER_X;
        slider_x->y = SLIDER_Y_LEN - SLIDER_WIDE;
        slider_x->len = SLIDER_X_LEN;
        slider_x->wide = SLIDER_WIDE;
        slider_x->value = 0;
        slider_x->value_max = _value;
        slider_x->box_len = SLIDER_BOX_LEN;
        slider_x->box_speed = SLIDER_FLASH_S_MAX;
    }
    return slider_x;
}

void menu_slider_free(SLIDER_str * slider_y)
{
    if(slider_y!= NULL)
    {
        free(slider_y);
    }
}

void menu_slider_xy_draw(SLIDER_str * slider_y)
{
    uint8_t box_x = 0; 
    uint8_t box_y = 0; 
    uint8_t line_x = 0; 
    uint8_t line_y = 0; 
    
    if(slider_y!= NULL)
    {
        if(slider_y->x_y == SLIDER_MODE_X)  
        {
            line_y = slider_y->y + ((slider_y->wide) / 2);
            drawLine(slider_y->x, line_y, slider_y->x + slider_y->len, line_y);
            if(slider_y->value > slider_y->value_max)
            {
                slider_y->value = slider_y->value_max;
            }
            box_x = slider_y->x + SLIDER_FLASH_X * slider_y->value;
            box_y = slider_y->y;
            oled_dis_rectangle(box_x, box_y, slider_y->box_len, slider_y->wide);

        }
        else if(slider_y->x_y == SLIDER_MODE_Y)  
        {
            line_x = slider_y->x + ((slider_y->wide) / 2);
            drawLine(line_x, slider_y->y, line_x, slider_y->y + slider_y->len);
            if(slider_y->value > slider_y->value_max)
            {
                slider_y->value = slider_y->value_max;
            }
            box_y = slider_y->y + SLIDER_FLASH_Y * slider_y->value;
            box_x = slider_y->x;
            oled_dis_rectangle(box_x, box_y, slider_y->wide, slider_y->box_len);
        }
    }
}

void menu_slider_y_speed_set(SLIDER_str * slider_y, uint8_t _speed)
{
    if(slider_y!= NULL)
    {
        if(_speed < SLIDER_FLASH_S_MAX && _speed > SLIDER_FLASH_S_MIN)
            slider_y->box_speed = _speed;
        else if(_speed >= SLIDER_FLASH_S_MAX)
            slider_y->box_speed = SLIDER_FLASH_S_MAX;
        else if(_speed <= SLIDER_FLASH_S_MIN)
            slider_y->box_speed = SLIDER_FLASH_S_MIN;
    }

}

#endif

#if 1 


const uint8_t function_list[] = 
{
    BMP_32_STATE,
    BMP_32_CONTROL,
    BMP_32_HEATING,
    BMP_32_REFLOW,
    BMP_32_SET,
};

const uint8_t txt_list[] = 
{
    TEXT_16_STATE,
    TEXT_16_CONTROL,
    TEXT_16_HEATING,
    TEXT_16_REFLOW,
    TEXT_16_SET,
};

typedef union {
	signed signed_byte;
    uint8_t byte;
    uint16_t word;
} DataUnion;

typedef struct string_value_STR
{
	char *str;
    union {
        DataUnion numeric;
        char str[8];
    } data;
}STRING_VALUE_STR;

STRING_VALUE_STR string_data_value[SET_STRING_NUMBER] =
{
    {"Set T1", {.numeric.byte = DEFAULT_T_TEMP_T1}},       // 0
    {"Set T2", {.numeric.byte = DEFAULT_T_TEMP_T2}},
    {"Set T3", {.numeric.byte = DEFAULT_T_TEMP_T3}},
    {"Set B1", {.numeric.byte = 45}},
    {"Set B2", {.numeric.byte = 50}},
    {"Set B3", {.numeric.byte = 60}},       // 5
    {"Set S1", {.numeric.byte = 150}},
    {"Set S2", {.numeric.byte = 180}},
    {"Set S3", {.numeric.byte = 210}},
    {"Temp Adj", {.numeric.signed_byte = DEFAULT_TEMP_ADJUST}},
    {"Set Kp", {.numeric.byte = 48}},    // 48
    {"Set Ki", {.numeric.byte = 1}},
    {"Set Kd", {.numeric.byte = 0}},
    {"Max Time", {.numeric.word = 480}},
    {"Max Power", {.numeric.byte = 100}},
    {"Reset", {.str = "NO"}},             // 15
    {"version", {.str = "1.01"}},         // 16
};

STRING_VALUE_STR string_set_control_data[2] = 
{
   {"Temp", {.numeric.byte = 90}},
   {"Time", {.numeric.word = 480}},
};

STRING_VALUE_STR  string_set_reflow_data[6] =
{
    {"Temp_1", {.numeric.byte = 150}},
    {"Time_1", {.numeric.word = 80}},
    {"Temp_2", {.numeric.byte = 180}},
    {"Time_2", {.numeric.word = 80}},
    {"Temp_3", {.numeric.byte = 230}},
    {"Time_3", {.numeric.word = 80}},
};
STRING_VALUE_STR string_dis_set_data = 
{
    .str = "NO ", {.numeric.byte = 0}
};

void set_u8_data(uint8_t _data, uint8_t * _data_p)
{
    if(_data_p!= NULL)
    {
        *_data_p = _data;
    }
}

void set_u16_data(uint16_t _data, uint16_t * _data_p)
{
    if(_data_p!= NULL)
    {
        *_data_p = _data;
    }
}

#if MENU_TEST
void menu_seting_dis_test(void)
{
    static uint8_t i = 0;

    if(i < TEST_SPEED)
    {
        i++;
    }
    else
    {
        i = 0;
        // dis_set_one++;
        // if(dis_set_one >= SET_STRING_NUMBER-3)
        // {
        //     dis_set_one = 0;
        // }
        // �����ƶ�
        dis_set_flash++;
        slider_y_set->value++;
        if(slider_y_set->value >= SET_STRING_NUMBER)
        {
            slider_y_set->value = 0;
            dis_set_flash = 0;
            dis_set_one = 0;
        }
        if(slider_y_set->value > 3)
        {
            dis_set_one++;
            // if(dis_set_one >= SET_STRING_NUMBER-6)
            // {
            //     dis_set_one = 0;
            // }
        }
    }
}
#endif
#if 1
void menu_seting_create(void)   // 100ms
{
    uint8_t i = 0;
    uint8_t leng = 0;
    
    for ( i = 0; i < 4; i++)       
    {
        if( (menu.current_row < VERSION))
        {
            if(menu.window_cnt+i == menu.current_row) 
            {
                oled_show_dis_w_string(SET_LABLE_X,SET_LABLE_Y_LENG*i,ASC2_0816,string_data_value[menu.window_cnt+i].str,strlen(string_data_value[menu.window_cnt+i].str));
            }
            else
            {
                oled_show_dis_string(SET_LABLE_X,SET_LABLE_Y_LENG*i,ASC2_0816,string_data_value[menu.window_cnt+i].str,strlen(string_data_value[menu.window_cnt+i].str));
            }
        }

        if(menu.window_cnt+i == VERSION)
        {
            oled_show_dis_string(SET_LABLE_X,SET_LABLE_Y_LENG*i,ASC2_0816,string_data_value[VERSION].str,strlen(string_data_value[VERSION].str));
            oled_show_dis_string(SET_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,string_data_value[VERSION].data.str,strlen(string_data_value[VERSION].data.str));
        }
        else
        if((menu.window_cnt+i) == SET_TEMP_ADJUST)
        {
            leng = data_to_str_8(string_data_value[menu.window_cnt+i].data.numeric.signed_byte,menu.dis_buf);
        }
        else if((menu.window_cnt+i) == SET_MAX_TIME)
        {
            leng = data_to_str_16(string_data_value[menu.window_cnt+i].data.numeric.word,menu.dis_buf);
        }
        else
        {
            leng = data_to_str_16(string_data_value[menu.window_cnt+i].data.numeric.byte,menu.dis_buf);
        }

        if(menu.current_row == menu.window_cnt+i) 
        {
            if((menu.set_data_ok == 0))   
            {
                if( menu.set_blink_cnt < menu.set_blink_light)  
                {
                    if((menu.window_cnt+i) ==  SET_RESET)
                    {
                        leng = 3;
                        oled_show_dis_string(SET_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,string_data_value[menu.window_cnt+i].data.str,leng);
                    }
                    else if((menu.window_cnt+i) == VERSION)
                    {
                        oled_show_dis_string(SET_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,menu.dis_buf,leng);
                    }
                    else if(((menu.window_cnt+i) > SET_S3_TEMP))
                    {
                        oled_show_dis_string(SET_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,menu.dis_buf,leng);
                    }

                    if((menu.window_cnt+i) == SET_TEMP_ADJUST)
                    {
                        oled_show_dis_char((SET_VALUE_X + 8 * leng),SET_LABLE_Y_LENG*i,CELSIUS_1616,0);    
                    }
                    else 
                    if((menu.window_cnt+i) == SET_MAX_TIME)
                    {
                        oled_show_dis_char((SET_VALUE_X + 8 * leng),SET_LABLE_Y_LENG*i,ASC2_0816,(DIS_FUHAO_ID('m')));
                    }
                    else if((menu.window_cnt+i) == SET_MAX_POWER)
                    {
                        oled_show_dis_char((SET_VALUE_X + 8 * leng),SET_LABLE_Y_LENG*i,ASC2_0816,(DIS_FUHAO_ID('%')));
                    }
                }
            }
            else
            {
                if((menu.window_cnt+i) ==  SET_RESET)
                {
                    leng = 3;
                    oled_show_dis_string(SET_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,string_data_value[menu.window_cnt+i].data.str,leng);
                }
                else if((menu.window_cnt+i) == VERSION)
                {
                    leng = 7;
                    oled_show_dis_string(SET_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,string_data_value[menu.window_cnt+i].data.str,leng);
                }
                else if((menu.window_cnt+i) > SET_S3_TEMP)
                {
                    oled_show_dis_string(SET_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,menu.dis_buf,leng);
                }

                if((menu.window_cnt+i) == SET_TEMP_ADJUST)
                {
                    oled_show_dis_char((SET_VALUE_X + 8 * leng),SET_LABLE_Y_LENG*i,CELSIUS_1616,0);       
                }
                else 
                if((menu.window_cnt+i) == SET_MAX_TIME)
                {
                    oled_show_dis_char((SET_VALUE_X + 8 * leng),SET_LABLE_Y_LENG*i,ASC2_0816,(DIS_FUHAO_ID('m')));
                }
                else if((menu.window_cnt+i) == SET_MAX_POWER)
                {
                    oled_show_dis_char((SET_VALUE_X + 8 * leng),SET_LABLE_Y_LENG*i,ASC2_0816,(DIS_FUHAO_ID('%')));
                }
            }
        }
        else
        {
            if((menu.window_cnt+i) ==  SET_RESET)
            {
                leng = 3;
                oled_show_dis_string(SET_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,string_data_value[menu.window_cnt+i].data.str,leng);
            }
            else if((menu.window_cnt+i) == VERSION)
            {

            }
            else if((menu.window_cnt+i) > SET_S3_TEMP)
            {
                oled_show_dis_string(SET_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,menu.dis_buf,leng);
            }

            if((menu.window_cnt+i) == SET_TEMP_ADJUST)
            {
                oled_show_dis_char((SET_VALUE_X + 8 * leng),SET_LABLE_Y_LENG*i,CELSIUS_1616,0);   
            }
            else if((menu.window_cnt+i) == SET_MAX_TIME)
            {
                oled_show_dis_char((SET_VALUE_X + 8 * leng),SET_LABLE_Y_LENG*i,ASC2_0816,(DIS_FUHAO_ID('m')));
            }
            else if((menu.window_cnt+i) == SET_MAX_POWER)
            {
                oled_show_dis_char((SET_VALUE_X + 8 * leng),SET_LABLE_Y_LENG*i,ASC2_0816,(DIS_FUHAO_ID('%')));
            }
        }
    }
    menu_slider_xy_draw(slider_y_set);

    #if MENU_TEST
    menu_seting_dis_test();
    #endif
}

#endif

#if 1

void menu_seting_time_create(void)
{
    uint8_t i = 0;
    uint8_t leng = 0;

    if(menu.set_time_ok == 0)
    {   
        BLINK_ON();
    }
    else
    {
        BLINK_OFF();
    }

    if(menu.set_data < SET_S1_TEMP)
    {
        for( i = 0; i < 2; i++)
        {
            if(i == menu.current_row)
            {
                oled_show_dis_w_string(SET_LABLE_X,SET_LABLE_Y_LENG*i,ASC2_0816,string_set_control_data[i].str,strlen(string_set_control_data[i].str));        
            }
            else
            {
                oled_show_dis_string(SET_LABLE_X,SET_LABLE_Y_LENG*i,ASC2_0816,string_set_control_data[i].str,strlen(string_set_control_data[i].str));    
            }

            if(i == menu.current_row)  
            {
                if(menu.set_blink_cnt < menu.set_blink_light)
                {
                    if(i == 0)
                    {
                        leng = data_to_str_16((string_set_control_data[i].data.numeric.byte),menu.dis_buf);
                    }
                    else
                    {
                        leng = data_to_str_16((string_set_control_data[i].data.numeric.word),menu.dis_buf);
                    }
                    
                    oled_show_dis_string(SET_TIME_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,menu.dis_buf,leng);
                }
            }
            else
            {
                if(i == 0)
                {
                    leng = data_to_str_16((string_set_control_data[i].data.numeric.byte),menu.dis_buf);
                }
                else
                {
                    leng = data_to_str_16((string_set_control_data[i].data.numeric.word),menu.dis_buf);
                }
                oled_show_dis_string(SET_TIME_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,menu.dis_buf,leng);
            }
            if(i == 0)
            {
                oled_show_dis_char((SET_TIME_VALUE_X + 8*3),SET_LABLE_Y_LENG*i,CELSIUS_1616,0);      
            }
            else
            {
                oled_show_dis_string((SET_TIME_VALUE_X + 8*3),SET_LABLE_Y_LENG*i,ASC2_0816,"min",3); 
            }
        }
    }
    else if(menu.set_data < SET_TEMP_ADJUST)
    { 
        for( i = 0; i < 4; i++)
        {
            if((i + menu.window_cnt) == menu.current_row)
            {
                oled_show_dis_w_string(SET_LABLE_X,SET_LABLE_Y_LENG*i,ASC2_0816,string_set_reflow_data[menu.window_cnt+i].str,strlen(string_set_reflow_data[menu.window_cnt+i].str));
            }
            else
            {
                oled_show_dis_string(SET_LABLE_X,SET_LABLE_Y_LENG*i,ASC2_0816,string_set_reflow_data[menu.window_cnt+i].str,strlen(string_set_reflow_data[menu.window_cnt+i].str));
            }

            if((i + menu.window_cnt) == menu.current_row)
            {
                if(menu.set_blink_cnt < menu.set_blink_light)
                {
                    leng = data_to_str_16((string_set_reflow_data[menu.window_cnt+i].data.numeric.byte),menu.dis_buf);
                    oled_show_dis_string(SET_TIME_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,menu.dis_buf,leng);      
                }
            }
            else
            {
                leng = data_to_str_16((string_set_reflow_data[menu.window_cnt+i].data.numeric.byte),menu.dis_buf);
                oled_show_dis_string(SET_TIME_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,menu.dis_buf,leng);
            }

            if((i + menu.window_cnt == 0) || (i + menu.window_cnt == 2) || (i + menu.window_cnt == 4))
            {
                if((i + menu.window_cnt) == menu.current_row)  
                {
                    if(menu.set_blink_cnt < menu.set_blink_light)
                    {
                        leng = data_to_str_16((string_set_reflow_data[menu.window_cnt+i].data.numeric.byte),menu.dis_buf);
                        oled_show_dis_string(SET_TIME_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,menu.dis_buf,leng);          
                    }
                }
                else
                {
                    leng = data_to_str_16((string_set_reflow_data[menu.window_cnt+i].data.numeric.byte),menu.dis_buf);
                    oled_show_dis_string(SET_TIME_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,menu.dis_buf,leng);
                }
                oled_show_dis_char((SET_TIME_VALUE_X + 8*3),SET_LABLE_Y_LENG*i,CELSIUS_1616,0); 
            }
            else
            {
                if((i + menu.window_cnt) == menu.current_row) 
                {
                    if(menu.set_blink_cnt < menu.set_blink_light)
                    {
                        leng = data_to_str_16((string_set_reflow_data[menu.window_cnt+i].data.numeric.word),menu.dis_buf);
                        oled_show_dis_string(SET_TIME_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,menu.dis_buf,leng);  
                    }
                }
                else
                {
                    leng = data_to_str_16((string_set_reflow_data[menu.window_cnt+i].data.numeric.word),menu.dis_buf);
                    oled_show_dis_string(SET_TIME_VALUE_X,SET_LABLE_Y_LENG*i,ASC2_0816,menu.dis_buf,leng);
                }
                oled_show_dis_string((SET_TIME_VALUE_X + 8*3),SET_LABLE_Y_LENG*i,ASC2_0816," s ",3); 
            }
        }
    }
}

#endif

#if MENU_TEST
void menu_scan_test(void)
{
    static uint8_t i = 0;
    if(i < 5)       // ����ʱ��
    {
        i++;
    }
    else
    {
        i = 0;
        if(function < FUNCTION_SET)
        {
            function++;
        }
        else
        {
            function = FUNCTION_STATE;
        }
    }
}
#endif

void menu_data_updata(void)
{
    // menu.set_temp = menu.set_temp_buf;
}

void menu_seting_data_init(void)
{
    Heating_Platform.work_time = MAX_WORK_TIME;

    Heating_Platform.stHeating_Control[0].set_control_temp = DEFAULT_T_TEMP_T1;
    Heating_Platform.stHeating_Control[0].set_work_time = DEFAULT_T_TIME_T1;

    Heating_Platform.stHeating_Control[1].set_control_temp = DEFAULT_T_TEMP_T2;
    Heating_Platform.stHeating_Control[1].set_work_time = DEFAULT_T_TIME_T2;

    Heating_Platform.stHeating_Control[2].set_control_temp = DEFAULT_T_TEMP_T3;
    Heating_Platform.stHeating_Control[2].set_work_time = DEFAULT_T_TIME_T3;

    Heating_Platform.stHeating_Control[3].set_control_temp = DEFAULT_B_TEMP_T1;
    Heating_Platform.stHeating_Control[3].set_work_time = DEFAULT_B_TIME_T1;

    Heating_Platform.stHeating_Control[4].set_control_temp = DEFAULT_B_TEMP_T2;
    Heating_Platform.stHeating_Control[4].set_work_time = DEFAULT_B_TIME_T2;

    Heating_Platform.stHeating_Control[5].set_control_temp = DEFAULT_B_TEMP_T3;
    Heating_Platform.stHeating_Control[5].set_work_time = DEFAULT_B_TIME_T3;

    Heating_Platform.stReflowSoldering[0].stHeating_Phases[0].set_control_temp = DEFAULT_S1_TEMP_T1;
    Heating_Platform.stReflowSoldering[0].stHeating_Phases[1].set_control_temp = DEFAULT_S1_TEMP_T2;
    Heating_Platform.stReflowSoldering[0].stHeating_Phases[2].set_control_temp = DEFAULT_S1_TEMP_T3;
    Heating_Platform.stReflowSoldering[0].stHeating_Phases[0].set_control_time = DEFAULT_S1_TIME_T1;
    Heating_Platform.stReflowSoldering[0].stHeating_Phases[1].set_control_time = DEFAULT_S1_TIME_T2;
    Heating_Platform.stReflowSoldering[0].stHeating_Phases[2].set_control_time = DEFAULT_S1_TIME_T3;


    Heating_Platform.stReflowSoldering[1].stHeating_Phases[0].set_control_temp = DEFAULT_S2_TEMP_T1;
    Heating_Platform.stReflowSoldering[1].stHeating_Phases[1].set_control_temp = DEFAULT_S2_TEMP_T2;
    Heating_Platform.stReflowSoldering[1].stHeating_Phases[2].set_control_temp = DEFAULT_S2_TEMP_T3;
    Heating_Platform.stReflowSoldering[1].stHeating_Phases[0].set_control_time = DEFAULT_S2_TIME_T1;
    Heating_Platform.stReflowSoldering[1].stHeating_Phases[1].set_control_time = DEFAULT_S2_TIME_T2;
    Heating_Platform.stReflowSoldering[1].stHeating_Phases[2].set_control_time = DEFAULT_S2_TIME_T3;

    Heating_Platform.stReflowSoldering[2].stHeating_Phases[0].set_control_temp = DEFAULT_S3_TEMP_T1;
    Heating_Platform.stReflowSoldering[2].stHeating_Phases[1].set_control_temp = DEFAULT_S3_TEMP_T2;
    Heating_Platform.stReflowSoldering[2].stHeating_Phases[2].set_control_temp = DEFAULT_S3_TEMP_T3;
    Heating_Platform.stReflowSoldering[2].stHeating_Phases[0].set_control_time = DEFAULT_S3_TIME_T1;
    Heating_Platform.stReflowSoldering[2].stHeating_Phases[1].set_control_time = DEFAULT_S3_TIME_T2;
    Heating_Platform.stReflowSoldering[2].stHeating_Phases[2].set_control_time = DEFAULT_S3_TIME_T3;

    Heating_Platform.calibrate_temp = DEFAULT_TEMP_ADJUST;

    Heating_Platform.Kp = DEFAULT_KP;
    Heating_Platform.Ki = DEFAULT_KI;
    Heating_Platform.Kd = DEFAULT_KD;

    Heating_Platform.max_power = DEFAULT_MAX_POWER;
}

void menu_seting_function_data(void)
{
    if(menu.set_data <= SET_B3_TEMP)
    {
        if(menu.set_time == SET_TEMP)
        {
            Heating_Platform.stHeating_Control[menu.set_data].set_control_temp = string_set_control_data[SET_TEMP].data.numeric.byte;    
        }
        else if(menu.set_time == SET_TIME)
        {
            Heating_Platform.stHeating_Control[menu.set_data].set_work_time = string_set_control_data[SET_TIME].data.numeric.word;    
        }
        Heating_Platform.flag_write_flash = 1;
    }
    else if(menu.set_data <= SET_S3_TEMP)
    {
        Heating_Platform.flag_write_flash = 1;
        switch (menu.set_time)
        {
            case SET_TEMP_1:
            {
                Heating_Platform.stReflowSoldering[menu.set_data - SET_S1_TEMP].stHeating_Phases[0].set_control_temp = string_set_reflow_data[SET_TEMP_1].data.numeric.byte;
                break;
            }
            case SET_TEMP_2:
            {
                Heating_Platform.stReflowSoldering[menu.set_data - SET_S1_TEMP].stHeating_Phases[1].set_control_temp = string_set_reflow_data[SET_TEMP_2].data.numeric.byte;
                break;
            }
            case SET_TEMP_3:
            {
                Heating_Platform.stReflowSoldering[menu.set_data - SET_S1_TEMP].stHeating_Phases[2].set_control_temp = string_set_reflow_data[SET_TEMP_3].data.numeric.byte;
                break;
            }
            case SET_TIME_1:
            {
                Heating_Platform.stReflowSoldering[menu.set_data - SET_S1_TEMP].stHeating_Phases[0].set_control_time = string_set_reflow_data[SET_TIME_1].data.numeric.word;
                break;
            }
            case SET_TIME_2:
            {
                Heating_Platform.stReflowSoldering[menu.set_data - SET_S1_TEMP].stHeating_Phases[1].set_control_time = string_set_reflow_data[SET_TIME_2].data.numeric.word;
                break;
            }
            case SET_TIME_3:
            {
                Heating_Platform.stReflowSoldering[menu.set_data - SET_S1_TEMP].stHeating_Phases[2].set_control_time = string_set_reflow_data[SET_TIME_3].data.numeric.word;
                break;
            }
            default:
            {
                Heating_Platform.flag_write_flash = 0;
                break;
            }
        }
    }
}

void menu_dis_function_data(void)
{
    if(menu.set_data <= SET_B3_TEMP)
    {
        string_set_control_data[SET_TEMP].data.numeric.byte = Heating_Platform.stHeating_Control[menu.set_data].set_control_temp;
        string_set_control_data[SET_TIME].data.numeric.word = Heating_Platform.stHeating_Control[menu.set_data].set_work_time;
    }
    else if(menu.set_data <= SET_S3_TEMP)
    {
        string_set_reflow_data[SET_TEMP_1].data.numeric.byte = Heating_Platform.stReflowSoldering[menu.set_data - SET_S1_TEMP].stHeating_Phases[0].set_control_temp;
        string_set_reflow_data[SET_TEMP_2].data.numeric.byte = Heating_Platform.stReflowSoldering[menu.set_data - SET_S1_TEMP].stHeating_Phases[1].set_control_temp;
        string_set_reflow_data[SET_TEMP_3].data.numeric.byte = Heating_Platform.stReflowSoldering[menu.set_data - SET_S1_TEMP].stHeating_Phases[2].set_control_temp;
        string_set_reflow_data[SET_TIME_1].data.numeric.word = Heating_Platform.stReflowSoldering[menu.set_data - SET_S1_TEMP].stHeating_Phases[0].set_control_time;
        string_set_reflow_data[SET_TIME_2].data.numeric.word = Heating_Platform.stReflowSoldering[menu.set_data - SET_S1_TEMP].stHeating_Phases[1].set_control_time;
        string_set_reflow_data[SET_TIME_3].data.numeric.word = Heating_Platform.stReflowSoldering[menu.set_data - SET_S1_TEMP].stHeating_Phases[2].set_control_time;
    }
}

void menu_seting_function_set_data(void)
{
    Heating_Platform.flag_write_flash = 1;
    switch (menu.set_data)
    {
        case SET_TEMP_ADJUST:
        {
            Heating_Platform.calibrate_temp = string_data_value[SET_TEMP_ADJUST].data.numeric.byte;
            Heating_set_adjust_temp(&Heating,Heating_Platform.calibrate_temp);
            break;
        }

        case SET_KP:
        case SET_KI:
        case SET_KD:
        {
            Heating_Platform.Kp = string_data_value[SET_KP].data.numeric.byte;
            Heating_Platform.Ki = string_data_value[SET_KI].data.numeric.byte;
            Heating_Platform.Kd = string_data_value[SET_KD].data.numeric.byte;
            IncrementalPID_Init(Heating.control_pid, Heating_Platform.Kp, Heating_Platform.Ki, Heating_Platform.Kd);
            break;
        }

        case SET_MAX_TIME:
        {
            Heating_Platform.work_time = string_data_value[SET_MAX_TIME].data.numeric.word;
            
            break;
        }

        case SET_MAX_POWER:
        {
            Heating_Platform.max_power = string_data_value[SET_MAX_POWER].data.numeric.byte;
            Heating_set_power_max(&Heating,Heating_Platform.max_power);
            break;
        }

        case SET_RESET:
        {
            break;
        }

        default:
        {
            Heating_Platform.flag_write_flash = 0;
            break;
        }
            
    }
}

void menu_scan(void) 
{
    uint8_t box_x = 0;

    if(menu.set_blink_cnt >= BLINK_CYCLE)
    {
        menu.set_blink_cnt = 0;
    }
    else
    {
        menu.set_blink_cnt++;
    }

    if(menu.layer == MENU_LAYER_SW)     
    {
        if(menu.function == FUNCTION_REFLOW)
        {
            box_x = 42;
        }
        else
        {
            box_x = 48;
        }

        oled_show_dis_ID(12,8,BMP_32_LEFT);
        oled_show_dis_ID(48,8,function_list[menu.function]);    
        oled_show_dis_ID(80,8,BMP_32_RIGHT);
        oled_show_dis_ID_Text(box_x,48,txt_list[menu.function]);
    }
    else if(menu.layer == MENU_LAYER_SET)
    {
        BLINK_OFF();
        menu_seting_create();
    }
    else if(menu.layer == MENU_LAYER_SET_MODE)
    {
        BLINK_ON();
        if(menu.function == FUNCTION_STATE)
        {
            menu_function_create(NO_WORK); 
        }
        else
        {
            menu_function_create(menu.work); 
        }
        
    }
    else if(menu.layer == MENU_LAYER_SET_DATA)
    {   
        BLINK_ON();
        menu_seting_create();
    }
    else if (menu.layer == MENU_LAYER_SET_TIME)
    {
        menu_seting_time_create();
    }
    else if(menu.layer == MENU_LAYER_WORK)
    {
        BLINK_OFF();
        menu_function_create(menu.work);
    }

    #if MENU_TEST
    menu_scan_test();
    #endif
}

void menu_next_set_layer(void)
{
    if(menu.layer == MENU_LAYER_SW)
    {
        menu.layer = MENU_LAYER_SET;
    }
}

void menu_next_set_data_layer(void)
{
    uint8_t i = 0;
    if((menu.layer == MENU_LAYER_SET))
    {
        if(menu.function == FUNCTION_SET)
        {
            if((menu.set_data > NO_WORK) && (menu.set_data < SET_MAX_TIME))
            {
                menu.layer = MENU_LAYER_SET_TIME;
                menu.set_time_ok = 1;             
            }
            else
            {
                menu.layer = MENU_LAYER_SET_DATA;
                if(menu.set_data == SET_MAX_TIME)
                {
                    string_dis_set_data.data.numeric.word = string_data_value[menu.set_data].data.numeric.word;
                }
                else if(menu.set_data == SET_RESET)
                {
                    while(string_data_value[menu.set_data].str[i] != '\0')
                    {
                        string_dis_set_data.str[i] = string_data_value[menu.set_data].str[i];
                        i++;
                    }
                }
                else
                {
                    string_dis_set_data.data.numeric.byte = string_data_value[menu.set_data].data.numeric.byte;
                }
            }
        }
    }
}

void menu_new_windows( void)
{
    if(menu.layer == MENU_LAYER_SET)
    {
        menu.current_row = menu.set_data;  
        menu.window_cnt = menu.current_row;
        if(menu.window_cnt > SET_MAX_TIME)
        {
            menu.window_cnt = SET_MAX_TIME;
        }
    }
    else if((menu.layer == MENU_LAYER_SET_TIME) || (menu.layer == MENU_LAYER_SW))
    {
        menu.current_row = 0;
        menu.window_cnt = 0;
    }
}

void Heating_start_work(void)
{
    if(Heating_Platform.mode <= HEATING_B3)
    {
        Heating.set_temp = Heating_Platform.stHeating_Control[Heating_Platform.mode - CONTROL_T1].set_control_temp;
    }
    else if(Heating_Platform.mode <= REFLOW_S3)
    {
        Heating.set_temp = Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].stHeating_Phases[0].set_control_temp; // 设置第一阶段温度
        Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].stHeating_Phases[0].heating_time = Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].stHeating_Phases[0].set_control_time; // 设置第一阶段工作时间       
        Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].set_control_status = 0;
    }
    // printf("Heating.set_temp = %d\r\n",Heating.set_temp);
    // printf("heating_time = %d\r\n",Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].stHeating_Phases[0].heating_time);
    Heating_Set_Temp(&Heating, Heating.set_temp);
    Heating_Start(&Heating);
}

void Heating_stop_work(void)
{
    menu.layer = MENU_LAYER_SW;
    menu.function = FUNCTION_STATE;
    Heating_Platform.mode = 0;
    menu.work = (WORK_ENUM)Heating_Platform.mode;
    Heating_Stop(&Heating);
    Heating_Clear_Time(&Heating);
}

void Heating_time_init(void)
{
    if((menu.work > NO_WORK) && (menu.work <= HEATING_B3))
    {
        Heating_Platform.heat_time = Heating_Platform.stHeating_Control[menu.work - CONTROL_T1].set_work_time * 60;
    }
    else if(menu.work <= REFLOW_S3)
    {
        Heating_Platform.heat_time = Heating_Platform.stReflowSoldering[menu.work - REFLOW_S1].stHeating_Phases[0].set_control_time
        + Heating_Platform.stReflowSoldering[menu.work - REFLOW_S1].stHeating_Phases[1].set_control_time
        + Heating_Platform.stReflowSoldering[menu.work - REFLOW_S1].stHeating_Phases[2].set_control_time;

        Heating_Platform.stReflowSoldering[menu.work - REFLOW_S1].stHeating_Phases[0].heating_time = 
            Heating_Platform.stReflowSoldering[menu.work - REFLOW_S1].stHeating_Phases[0].set_control_time;
        Heating_Platform.stReflowSoldering[menu.work - REFLOW_S1].stHeating_Phases[1].heating_time = 
            Heating_Platform.stReflowSoldering[menu.work - REFLOW_S1].stHeating_Phases[1].set_control_time;
        Heating_Platform.stReflowSoldering[menu.work - REFLOW_S1].stHeating_Phases[2].heating_time = 
            Heating_Platform.stReflowSoldering[menu.work - REFLOW_S1].stHeating_Phases[2].set_control_time;
    }
}

void Heating_Time_Deal_1000ms(void)
{
    if(Heating_Platform.mode > 0)  
    {
        if(Heating_Platform.heat_time > 0)
        {
            Heating_Platform.heat_time--;
        }
        else
        {
            Heating_stop_work();
            menu.layer = MENU_LAYER_SET_MODE;
            menu.function = FUNCTION_STATE;
            Heating_Platform.flag_heating_over_time = 1;
        }

        if(Heating_Platform.mode >= REFLOW_S1)  // 回流焊功能
        {
            if(Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].stHeating_Phases[Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].set_control_status].heating_time > 0)
            {
                Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].stHeating_Phases[Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].set_control_status].heating_time--;
                if(Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].stHeating_Phases[Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].set_control_status].heating_time == 0)
                {
                    if(Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].set_control_status < 2)
                    {
                        Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].set_control_status++;
                    }
                    Heating.set_temp = Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].stHeating_Phases[Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].set_control_status].set_control_temp;
                    Heating_Set_Temp(&Heating, Heating.set_temp);
                    Heating_Start(&Heating);
                }
            }
            // printf("----mode = %d\r\n",Heating_Platform.mode);
            // printf("----set_control_status = %d\r\n",Heating_Platform.stReflowSoldering[Heating_Platform.mode - REFLOW_S1].set_control_status);
            // printf("----Heating_Set_Temp = %d\r\n",Heating.set_temp);
        }
    }
}

void menu_next_layer(void)
{
    if(menu.layer < MENU_LAYER_WORK)
    {
        if(menu.layer == MENU_LAYER_SW)
        {
            if(menu.function < FUNCTION_SET)
            {
                menu.layer = MENU_LAYER_SET_MODE;       
                BLINK_ON();
                if(menu.function == FUNCTION_CONTROL)
                {
                    menu.set_data_ok = 0;               
                    menu.work = CONTROL_T1;
                }
                else if(menu.function == FUNCTION_HEATING)
                {
                    menu.set_data_ok = 0;               
                    menu.work = HEATING_B1;
                }
                else if(menu.function == FUNCTION_REFLOW)
                {
                    menu.set_data_ok = 0;               
                    menu.work = REFLOW_S1;
                }
                Heating_time_init();
            }
            else
            {
                menu.layer = MENU_LAYER_SET;            
                menu.set_data = SET_T1_TEMP;
                menu_new_windows();
                slider_y_set->value = 0;
                BLINK_OFF(); 
            }
            menu.layer_last = menu.layer;

        }
        else if(menu.layer == MENU_LAYER_SET)
        {
            if( (menu.set_data <= SET_S3_TEMP))
            {
                menu.layer = MENU_LAYER_SET_TIME;
                menu.set_time_ok = 1;
                menu.set_time = SET_TEMP;
                menu_new_windows();
                menu_dis_function_data();
                
            }
            else if(menu.set_data < VERSION)
            {
                menu.layer = MENU_LAYER_SET_DATA;
                Heating_time_init();
                menu_new_windows();
                BLINK_ON();
            }
        }
        else if(menu.layer == MENU_LAYER_SET_MODE)
        {
            menu.layer = MENU_LAYER_WORK;   
            menu.work_OK = menu.work;
            Heating_Platform.mode = menu.work;
            Heating_start_work();
            Heating_time_init();
        }
        else if(menu.layer == MENU_LAYER_SET_DATA)       
        {
            menu.layer = MENU_LAYER_SET;
            menu_seting_function_set_data();
            
        }
        else if(menu.layer == MENU_LAYER_SET_TIME)
        {
            if(menu.set_time_ok == 0)
            {
                menu.set_time_ok = 1;         
                menu_seting_function_data();
            }
            else
            {
                menu.set_time_ok = 0;
                menu_dis_function_data();
            }
        }
    }
}

void menu_last_layer(void)
{
    if(menu.layer > MENU_LAYER_SW)
    {
        if(menu.layer == MENU_LAYER_WORK)
        {
            menu.layer = MENU_LAYER_SW;
            menu.work = NO_WORK;
            Heating_Platform.mode = menu.work;
            Heating_stop_work();
        }
        else if((menu.layer == MENU_LAYER_SET) || (menu.layer == MENU_LAYER_SET_MODE))
        {
            menu.layer = MENU_LAYER_SW;
        }
        else if((menu.layer == MENU_LAYER_SET_TIME) || (menu.layer == MENU_LAYER_SET_DATA))
        {
            menu.layer = MENU_LAYER_SET;
            menu.set_time_ok = 1;
            menu_new_windows();
        }
    }

}

void menu_set_data_add(uint8_t _data, uint8_t _add) 
{
    if(_data == SET_MAX_TIME)
    {
        if(string_data_value[_data].data.numeric.word < MAX_TIME + 1 - _add)
        {
            string_data_value[_data].data.numeric.word += _add;
        }
        else
        {
            string_data_value[_data].data.numeric.word = MAX_TIME;
        }
        
    }
    else if(_data == SET_RESET)
    {
        if(_add == 1)
        {
            if(string_data_value[_data].data.str[0] == 'N')
            {
                string_data_value[_data].data.str[0] = 'Y';
                string_data_value[_data].data.str[1] = 'E';
                string_data_value[_data].data.str[2] = 'S';
                string_data_value[_data].data.str[3] = '\0';
            }
            else
            {
                string_data_value[_data].data.str[0] = 'N';
                string_data_value[_data].data.str[1] = 'O';
                string_data_value[_data].data.str[2] = '\0';
                string_data_value[_data].data.str[3] = '\0';
            }
        }
    }
    else
    {
        switch(_data)
        {
            case SET_TEMP_ADJUST:
            {
                if(string_data_value[_data].data.numeric.signed_byte < MAX_TEMP_ADJUST +1 - _add)
                {
                    string_data_value[_data].data.numeric.signed_byte += _add;
                }
                else
                {
                    string_data_value[_data].data.numeric.signed_byte = MAX_TEMP_ADJUST;
                }
                break;
            }

            case SET_KP:
            {
                if(string_data_value[_data].data.numeric.byte < MAX_KP +1 - _add)
                {
                    string_data_value[_data].data.numeric.byte += _add;
                }
                else
                {
                    string_data_value[_data].data.numeric.byte = MAX_KP;
                }
                break;
            }

            case SET_KI:
            {
                if(string_data_value[_data].data.numeric.byte < MAX_KI +1 - _add)
                {
                    string_data_value[_data].data.numeric.byte += _add;
                }
                else
                {
                    string_data_value[_data].data.numeric.byte = MAX_KI;
                }
                break;
            }

            case SET_KD:
            {
                if(string_data_value[_data].data.numeric.byte < MAX_KD +1 - _add)
                {
                    string_data_value[_data].data.numeric.byte += _add;
                }
                else
                {
                    string_data_value[_data].data.numeric.byte = MAX_KD;
                }
                break;
            }

            case SET_MAX_TIME:
            {
                if(string_data_value[_data].data.numeric.word > MAX_TIME + 1 - _add)
                {
                    string_data_value[_data].data.numeric.word += _add;
                }
                else
                {
                    string_data_value[_data].data.numeric.word = MAX_TIME;
                }
                break;
            }

            case SET_MAX_POWER:
            {
                if(string_data_value[_data].data.numeric.byte < MAX_POWER +1 - _add)
                {
                    string_data_value[_data].data.numeric.byte += _add;
                }
                else
                {
                    string_data_value[_data].data.numeric.byte = MAX_POWER;
                }
                break;
            }

            default:
            {
                break;
            }
        }
    }
}

void menu_set_data_sub(uint8_t _data, uint8_t _sub)
{
    if(_data == SET_MAX_TIME)
    {
        if(string_data_value[_data].data.numeric.word > MIN_TIME + 1 + _sub)
        {
            string_data_value[_data].data.numeric.word -= _sub;
        }
        else
        {
            string_data_value[_data].data.numeric.word = MIN_TIME;
        }
    }
    else if(_data == SET_RESET)
    {
        if(_sub == 1)
        {
            if(string_data_value[_data].data.str[0] != 'N')
            {
                string_data_value[_data].data.str[0] = 'N';
                string_data_value[_data].data.str[1] = 'O';
                string_data_value[_data].data.str[2] = '\0';
            }
            else
            {
                string_data_value[_data].data.str[0] = 'Y';
                string_data_value[_data].data.str[1] = 'E';
                string_data_value[_data].data.str[2] = 'S';
                string_data_value[_data].data.str[3] = '\0';
            }
        }
    }
    else
    {
        switch(_data)
        {
            
            case SET_TEMP_ADJUST:
            {
                if(string_data_value[_data].data.numeric.signed_byte > MIN_TEMP_ADJUST +1 + _sub)
                {
                    string_data_value[_data].data.numeric.signed_byte -= _sub;
                }
                else
                {
                    string_data_value[_data].data.numeric.signed_byte = MIN_TEMP_ADJUST;
                }
                break;
            }
            case SET_KP:
            {
                if(string_data_value[_data].data.numeric.byte > MIN_KP +1 + _sub)
                {
                    string_data_value[_data].data.numeric.byte -= _sub;
                }
                else
                {
                    string_data_value[_data].data.numeric.byte = MIN_KP;
                }
                break;
            }
            case SET_KI:
            {
                if(string_data_value[_data].data.numeric.byte > MIN_KI +1 + _sub)
                {
                    string_data_value[_data].data.numeric.byte -= _sub;
                }
                else
                {
                    string_data_value[_data].data.numeric.byte = MIN_KI;
                }
                break;
            }
            case SET_KD:
            {
                if(string_data_value[_data].data.numeric.byte > MIN_KD +1 + _sub)
                {
                    string_data_value[_data].data.numeric.byte -= _sub;
                }
                else
                {
                    string_data_value[_data].data.numeric.byte = MIN_KD;
                }
                break;
            }
            case SET_MAX_POWER:
            {
                if(string_data_value[_data].data.numeric.byte > MIN_POWER +1 + _sub)
                {
                    string_data_value[_data].data.numeric.byte -= _sub;
                }
                else
                {
                    string_data_value[_data].data.numeric.byte = MIN_POWER;
                }
                break;
            }
            default:
            {
                break;
            }
        }
    }
}

void menu_set_list_add(uint8_t _add)
{
    if(menu.function == FUNCTION_SET)
    {
        if(menu.window_cnt != menu.current_row)
        {
            
        }
        else
        {
            menu.window_cnt += _add;
            if((menu.window_cnt + 3) >= (SET_MAX - 1))
            {
                menu.window_cnt = SET_MAX - 4; 
            }
        }
        menu.current_row += _add;
        menu.set_data += _add;
    
        if(menu.current_row >= SET_MAX-1)
        {
            menu.current_row = SET_RESET;
            menu.set_data = SET_RESET;
        }
        slider_y_set->value = menu.current_row;
    }
    else 
    {
        #if 1
        switch(menu.function)
        {
            case FUNCTION_CONTROL:
            {
                if(menu.work < CONTROL_T3) 
                {
                    menu.work += 1;
                }
                else
                {
                    menu.work = CONTROL_T1;
                }
                break;
            }
            case FUNCTION_HEATING:
            {
                if(menu.work < HEATING_B3)
                {
                    menu.work += 1;
                }
                else
                {
                    menu.work = HEATING_B1;
                }
                break;
            }
            case FUNCTION_REFLOW:
            {
                if(menu.work < REFLOW_S3)
                {
                    menu.work += 1;
                }
                else
                {
                    menu.work = REFLOW_S1;
                }
                break;
            }
            default:
            {
                menu.work = NO_WORK;
                break;
            }
        }
        menu.set_work_ok = 0;
        #endif
    }
}

void menu_set_list_sub(uint8_t _sub)
{
    if(menu.function == FUNCTION_SET)
    {
        if(menu.window_cnt != menu.current_row)
        {

        }
        else
        {
            if(menu.window_cnt > SET_DATA_NUM_MIN + _sub -1)
            {
                menu.window_cnt -= _sub;
            }
            else
            {
                menu.window_cnt = SET_T1_TEMP;
            }
        }

        if(menu.current_row > SET_T1_TEMP + _sub - 1)
        {
            menu.current_row -= _sub;
            menu.set_data -= _sub;
        }
        else
        {
            menu.current_row = SET_T1_TEMP;
            menu.set_data = SET_T1_TEMP;
        }
        slider_y_set->value = menu.current_row;
    }
    else
    {
        #if 1
        switch(menu.function)
        {
            case FUNCTION_CONTROL:
            {
                if(menu.work > CONTROL_T1)
                {
                    menu.work -= 1;
                }
                else
                {
                    menu.work = CONTROL_T3;
                }
                break;
            }
            case FUNCTION_HEATING:
            {
                if(menu.work > HEATING_B1)
                {
                    menu.work -= 1;
                }
                else
                {
                    menu.work = HEATING_B3;
                }
                break;
            }
            case FUNCTION_REFLOW:
            {
                if(menu.work > REFLOW_S1)
                {
                    menu.work -= 1;
                }
                else
                {
                    menu.work = REFLOW_S3;
                }
                break;
            }
        }
        #endif
    }
}

const uint8_t max_temp[9] = {MAX_T_TEMP, MAX_T_TEMP, MAX_T_TEMP, MAX_B_TEMP, MAX_B_TEMP, MAX_B_TEMP, MAX_S_TEMP_1, MAX_S_TEMP_2, MAX_S_TEMP_3};
const uint16_t max_time[9] = {MAX_T_TIME, MAX_T_TIME, MAX_T_TIME, MAX_B_TIME, MAX_B_TIME, MAX_B_TIME, MAX_S_TIME_1, MAX_S_TIME_2, MAX_S_TIME_3};
const uint8_t min_temp[9] = {MIN_T_TEMP, MIN_T_TEMP, MIN_T_TEMP, MIN_B_TEMP, MIN_B_TEMP, MIN_B_TEMP, MIN_S_TEMP_1, MIN_S_TEMP_2, MIN_S_TEMP_3};
const uint16_t min_time[9] = {MIN_T_TIME, MIN_T_TIME, MIN_T_TIME, MIN_B_TIME, MIN_B_TIME, MIN_B_TIME, MIN_S_TIME_1, MIN_S_TIME_2, MIN_S_TIME_3};

void menu_add_layer(void)
{
    BLINK_CLE();
    if(menu.layer == MENU_LAYER_SW)
    {
        menu.function++;
        if(menu.function > FUNCTION_SET)
        {
            menu.function = FUNCTION_STATE;
        }
    }
    else if(menu.layer == MENU_LAYER_SET || menu.layer == MENU_LAYER_SET_MODE)
    {
        BLINK_CLE();
        menu_set_list_add(1);
        Heating_time_init();
    }
    else if(menu.layer == MENU_LAYER_SET_DATA)
    {
        BLINK_CLE();
        menu_set_data_add(menu.set_data, 1);
    }
    else if(menu.layer == MENU_LAYER_SET_TIME)
    {
        BLINK_CLE();
        if(menu.set_time_ok == 1)                
        {
            if(menu.set_data <= SET_B3_TEMP)
            {
                menu.set_time++;
                menu.current_row++;
                if(menu.set_time > SET_TIME)
                {
                    menu.set_time = SET_TEMP;
                    menu.current_row = SET_TEMP;
                }
            }
            else if(menu.set_data <= SET_S3_TEMP)
            {
                menu.set_time++;
                menu.current_row++;
                menu.window_cnt++;
                if(menu.window_cnt + 3 > SET_TIME_3)
                {
                    menu.window_cnt = SET_TIME_3 - 3;
                }
                if(menu.set_time > SET_TIME_3)
                {
                    menu.current_row = SET_TIME_3;
                    menu.set_time = (SET_TIME_ENUM)menu.current_row;
                }
            }
        }
        else
        {
            if(menu.set_data <= SET_B3_TEMP)
            {
                if(menu.set_time == SET_TEMP)
                {
                    if(string_set_control_data[menu.set_time].data.numeric.byte < max_temp[menu.set_data])
                    {
                        string_set_control_data[menu.set_time].data.numeric.byte += 1;

                    }
                    else
                    {
                        string_set_control_data[menu.set_time].data.numeric.byte = max_temp[menu.set_data];
                    }
                }
                else if(menu.set_time == SET_TIME)
                {
                    if(string_data_value[menu.set_time].data.numeric.word < max_time[menu.set_data])
                    {
                        string_set_control_data[menu.set_time].data.numeric.word += 1;
                    }
                    else
                    {
                        string_set_control_data[menu.set_time].data.numeric.word = max_time[menu.set_data];
                    }
                }
            }
            else if(menu.set_data <= SET_S3_TEMP)
            {
                if(menu.set_time == SET_TEMP_1 || menu.set_time == SET_TEMP_2 || menu.set_time == SET_TEMP_3)
                {
                    if(string_set_reflow_data[menu.set_time].data.numeric.byte < max_temp[menu.set_data])
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.byte += 1;

                    }
                    else
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.byte = max_temp[menu.set_data];
                    }
                }
                else if(menu.set_time == SET_TIME_1 || menu.set_time == SET_TIME_2 || menu.set_time == SET_TIME_3)
                {
                    if(string_set_reflow_data[menu.set_time].data.numeric.word < max_time[menu.set_data])
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.word += 1;
                    }
                    else
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.word = max_time[menu.set_data];
                    }
                }
            }
        }
    }
    else if(menu.layer == MENU_LAYER_WORK)
    {
        
    }
}

void menu_sub_layer(void)
{
    BLINK_CLE();
    if(menu.layer == MENU_LAYER_SW)
    {
        if(menu.function > FUNCTION_STATE)
        {
            menu.function--;
        }
        else
        {
            menu.function = FUNCTION_SET;
        }
    }
    else if((menu.layer == MENU_LAYER_SET) || (menu.layer == MENU_LAYER_SET_MODE))
    {
        BLINK_CLE();
        menu_set_list_sub(1);
        Heating_time_init();
    }
    else if(menu.layer == MENU_LAYER_SET_DATA)
    {
        BLINK_CLE();
        menu_set_data_sub(menu.set_data, 1);
        
    }
    else if(menu.layer == MENU_LAYER_SET_TIME)
    {
        BLINK_CLE();
        if(menu.set_time_ok == 1)                 
        {
            if(menu.set_data <= SET_B3_TEMP)
            {
                if(menu.set_time > SET_TEMP)
                {
                    menu.set_time--;
                    menu.current_row--;
                }
                else 
                {
                    menu.set_time = SET_TIME;
                    menu.current_row = SET_TIME;
                }
            }
            else if(menu.set_data <= SET_S3_TEMP)
            {
                if(menu.window_cnt != menu.current_row)
                {
                    
                }
                else
                {
                    if(menu.window_cnt > 0)
                    {
                        menu.window_cnt--;
                    }
                    else 
                    {
                        menu.window_cnt = 0;
                    }
                }
                
                if(menu.current_row > 0)
                {
                    menu.set_time--;
                    menu.current_row--;
                }
                else 
                {
                    menu.current_row = 0;
                    menu.set_time = (SET_TIME_ENUM)menu.current_row;
                }
            }
        }
        else
        {
            if(menu.set_data <= SET_B3_TEMP)
            {
                if(menu.set_time == SET_TEMP)
                {
                    if(string_set_control_data[menu.set_time].data.numeric.byte > min_temp[menu.set_data])
                    {
                        string_set_control_data[menu.set_time].data.numeric.byte -= 1;

                    }
                    else
                    {
                        string_set_control_data[menu.set_time].data.numeric.byte = min_temp[menu.set_data];
                    }
                }
                else if(menu.set_time == SET_TIME)
                {
                    if(string_data_value[menu.set_time].data.numeric.word > min_time[menu.set_data])
                    {
                        string_set_control_data[menu.set_time].data.numeric.word -= 1;
                    }
                    else
                    {
                        string_set_control_data[menu.set_time].data.numeric.word = min_time[menu.set_data];
                    }
                }
            }
            else if(menu.set_data <= SET_S3_TEMP)
            {
                if(menu.set_time == SET_TEMP_1 || menu.set_time == SET_TEMP_2 || menu.set_time == SET_TEMP_3)
                {
                    if(string_set_reflow_data[menu.set_time].data.numeric.byte > min_temp[menu.set_data])
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.byte -= 1;

                    }
                    else
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.byte = min_temp[menu.set_data];
                    }
                }
                else if(menu.set_time == SET_TIME_1 || menu.set_time == SET_TIME_2 || menu.set_time == SET_TIME_3)
                {
                    if(string_set_reflow_data[menu.set_time].data.numeric.word > min_time[menu.set_data])
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.word -= 1;
                    }
                    else
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.word = min_time[menu.set_data];
                    }
                }
            }
        }
    }
    else if(menu.layer == MENU_LAYER_WORK)
    {

    }
}

void menu_add_layer_long(void)
{
    BLINK_CLE();
    if(menu.layer == MENU_LAYER_SW)
    {
        menu.function++;
        if(menu.function > FUNCTION_SET)
        {
            menu.function = FUNCTION_STATE;
        }
    }
    else if(menu.layer == MENU_LAYER_SET || menu.layer == MENU_LAYER_SET_MODE)
    {
        menu_set_list_add(1);
        Heating_time_init();
    }
    else if(menu.layer == MENU_LAYER_SET_TIME)
    {
        if(menu.set_time_ok == 1)   
        {
            if(menu.set_data <= SET_B3_TEMP)
            {
                menu.set_time++;
                menu.current_row++;
                if(menu.set_time > SET_TIME)
                {
                    menu.set_time = SET_TEMP;
                    menu.current_row = SET_TEMP;
                }
            }
            else if(menu.set_data <= SET_S3_TEMP)
            {
                menu.set_time++;
                menu.current_row++;
                menu.window_cnt++;
                if(menu.window_cnt + 3 > SET_TIME_3)
                {
                    menu.window_cnt = SET_TIME_3 - 3;
                }
                if(menu.set_time > SET_TIME_3)
                {
                    menu.current_row = SET_TIME_3;
                    menu.set_time = (SET_TIME_ENUM)menu.current_row;
                }
            }
        }
        else
        {
            if(menu.set_data <= SET_B3_TEMP)
            {
                if(menu.set_time == SET_TEMP)
                {
                    if(string_set_control_data[menu.set_time].data.numeric.byte < max_temp[menu.set_data])
                    {
                        string_set_control_data[menu.set_time].data.numeric.byte += 1;

                    }
                    else
                    {
                        string_set_control_data[menu.set_time].data.numeric.byte = max_temp[menu.set_data];
                    }
                }
                else if(menu.set_time == SET_TIME)
                {
                    if(string_data_value[menu.set_time].data.numeric.word < max_time[menu.set_data])
                    {
                        string_set_control_data[menu.set_time].data.numeric.word += 1;
                    }
                    else
                    {
                        string_set_control_data[menu.set_time].data.numeric.word = max_time[menu.set_data];
                    }
                }
            }
            else if(menu.set_data <= SET_S3_TEMP)
            {
                if(menu.set_time == SET_TEMP_1 || menu.set_time == SET_TEMP_2 || menu.set_time == SET_TEMP_3)
                {
                    if(string_set_reflow_data[menu.set_time].data.numeric.byte < max_temp[menu.set_data])
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.byte += 1;

                    }
                    else
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.byte = max_temp[menu.set_data];
                    }
                }
                else if(menu.set_time == SET_TIME_1 || menu.set_time == SET_TIME_2 || menu.set_time == SET_TIME_3)
                {
                    if(string_set_reflow_data[menu.set_time].data.numeric.word < max_time[menu.set_data])
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.word += 1;
                    }
                    else
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.word = max_time[menu.set_data];
                    }
                }
            }
        }
    }
}

void menu_sub_layer_long(void)
{
    BLINK_CLE();
    if(menu.layer == MENU_LAYER_SW)
    {
        if(menu.function > FUNCTION_STATE)
        {
            menu.function--;
        }
        else
        {
            menu.function = FUNCTION_SET;
        }
    }
    else if(menu.layer == MENU_LAYER_SET_DATA)
    {
        menu_set_data_sub(menu.set_data, 1);
        
    }
    else if(menu.layer == MENU_LAYER_SET_TIME)
    {
        if(menu.set_time_ok == 1)                  
        {
            if(menu.set_data <= SET_B3_TEMP)
            {
                if(menu.set_time > SET_TEMP)
                {
                    menu.set_time--;
                    menu.current_row--;
                }
                else 
                {
                    menu.set_time = SET_TIME;
                    menu.current_row = SET_TIME;
                }
            }
            else if(menu.set_data <= SET_S3_TEMP)
            {
                if(menu.window_cnt != menu.current_row)
                {
                    
                }
                else
                {
                    if(menu.window_cnt > 0)
                    {
                        menu.window_cnt--;
                    }
                    else
                    {
                        menu.window_cnt = 0;
                    }
                }
                
                if(menu.current_row > 0)
                {
                    menu.set_time--;
                    menu.current_row--;
                }
                else 
                {
                    menu.current_row = 0;
                    menu.set_time = (SET_TIME_ENUM)menu.current_row;
                }
            }
        }
        else
        {
            if(menu.set_data <= SET_B3_TEMP)
            {
                if(menu.set_time == SET_TEMP)
                {
                    if(string_set_control_data[menu.set_time].data.numeric.byte > min_temp[menu.set_data])
                    {
                        string_set_control_data[menu.set_time].data.numeric.byte -= 1;

                    }
                    else
                    {
                        string_set_control_data[menu.set_time].data.numeric.byte = min_temp[menu.set_data];
                    }
                }
                else if(menu.set_time == SET_TIME)
                {
                    if(string_data_value[menu.set_time].data.numeric.word > min_time[menu.set_data])
                    {
                        string_set_control_data[menu.set_time].data.numeric.word -= 1;
                    }
                    else
                    {
                        string_set_control_data[menu.set_time].data.numeric.word = min_time[menu.set_data];
                    }
                }
            }
            else if(menu.set_data <= SET_S3_TEMP)
            {
                if(menu.set_time == SET_TEMP_1 || menu.set_time == SET_TEMP_2 || menu.set_time == SET_TEMP_3)
                {
                    if(string_set_reflow_data[menu.set_time].data.numeric.byte > min_temp[menu.set_data])
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.byte -= 1;

                    }
                    else
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.byte = min_temp[menu.set_data];
                    }
                }
                else if(menu.set_time == SET_TIME_1 || menu.set_time == SET_TIME_2 || menu.set_time == SET_TIME_3)
                {
                    if(string_set_reflow_data[menu.set_time].data.numeric.word > min_time[menu.set_data])
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.word -= 1;
                    }
                    else
                    {
                        string_set_reflow_data[menu.set_time].data.numeric.word = min_time[menu.set_data];
                    }
                }
            }
        }
    }

}



#endif

